package com.example.myapplication.found.model;

import com.example.myapplication.found.bean.StockBean;
import com.example.myapplication.found.bean.StockKlineBean;
import com.example.myapplication.found.contract.FenxiCallback;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import static java.util.Map.Entry.comparingByKey;
import static java.util.Map.Entry.comparingByValue;
import static java.util.stream.Collectors.toMap;

/**
 * @author: rongfeng
 * @date: 2022/2/25
 * @description 连续涨跌
 */
public class Fenxi3 extends BaseData {
    public static final int type = 11;
    FenxiCallback fenxiCallback;

    public Fenxi3(StockBean stockBean, StockBean weekStockBean) {
        super(stockBean, weekStockBean);
    }

    public FenxiCallback getFenxiCallback() {
        return fenxiCallback;
    }

    public void setFenxiCallback(FenxiCallback fenxiCallback) {
        this.fenxiCallback = fenxiCallback;
    }

    @Override
    public void start() {
        test1();
    }


    public static Map<Integer, Integer> mapZhang = new HashMap<>();
    public static Map<Integer, Integer> mapDie = new HashMap<>();
    public static Map<Integer, Integer> mapDieFu = new HashMap<>();
    public static String startDate;
    public static int maxSize;
    public static int lineTotal;
    public static int stockTotal;
    public static int tuishiTotal;
    public static String endDate;
    static List<String> codeList = new ArrayList<>();


    //涨跌比例
    private void test1() {
        if (!codeList.contains(fundCode)) {
            codeList.add(fundCode);
        } else {
            if (fenxiCallback != null) {
                fenxiCallback.fenxiCallback(1);
            }
            return;
        }
        stockTotal++;
        if (stockBean.klineList.size() > maxSize) {
            maxSize = stockBean.klineList.size();
            startDate = stockBean.klineList.get(0).date;
            endDate = stockBean.klineList.get(maxSize - 1).date;
        }

        int count = 0;
        int diefu = 0;
        boolean isUP = false;

        for (int i = 0; i < stockBean.klineList.size(); i++) {
            lineTotal++;
            StockKlineBean bean = stockBean.klineList.get(i);
            if (bean.chg >= 0) {
                if (!isUP) {
                    Integer num = mapDie.get(count);
                    if (num == null) {
                        num = 0;
                    }
                    mapDie.put(count, ++num);


                    count = 0;
                    Integer diefunum = mapDieFu.get(diefu);
                    if (diefunum == null) {
                        diefunum = 0;
                    }
                    mapDieFu.put(diefu, ++diefunum);
                    diefu = 0;
                }
                isUP = true;
                count++;
            } else if (bean.chg < 0) {
                if (isUP) {
                    Integer num = mapZhang.get(count);
                    if (num == null) {
                        num = 0;
                    }
                    mapZhang.put(count, ++num);
                    count = 0;
                }
                diefu = diefu + (int) bean.chg;
                isUP = false;
                count++;
            }
        }
        if (fenxiCallback != null) {
            fenxiCallback.fenxiCallback(1);
        }
    }


    public static void analysis() {
        System.out.println("A股所有股票连续涨跌天数统计");
        System.out.println("统计开始日期： " + Fenxi3.startDate + " 截止日期：" + endDate);
        System.out.println("统计股票数量：" + Fenxi3.stockTotal + " 支");
        System.out.println("总统计次数：" + Fenxi3.lineTotal);
        System.out.println();

        mapZhang = mapZhang
                .entrySet()
                .stream()
                .sorted(comparingByKey())
                .collect(
                        toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e2, LinkedHashMap::new));

        Iterator<Map.Entry<Integer, Integer>> it1 = mapZhang.entrySet().iterator();

        while (it1.hasNext()) {
            Map.Entry<Integer, Integer> entry = it1.next();
            System.out.println("连涨: " + entry.getKey() + "  次数: " + entry.getValue());
        }
        System.out.println(" ");

        mapDie = mapDie.entrySet()
                .stream()
                .sorted(comparingByKey())
                .collect(toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e2, LinkedHashMap::new));

        Iterator<Map.Entry<Integer, Integer>> it2 = mapDie.entrySet().iterator();
        while (it2.hasNext()) {
            Map.Entry<Integer, Integer> entry = it2.next();
            System.out.println("连跌: " + entry.getKey() + "  次数: " + entry.getValue());
        }
    }


    public static double median(List<Float> total) {
        if (total.size() <= 0) {
            return 0;
        }
        double j = 0;
        //集合排序
        Collections.sort(total);
        int size = total.size();
        if (size % 2 == 1) {
            j = total.get((size - 1) / 2);
        } else {
            //加0.0是为了把int转成double类型，否则除以2会算错
            j = (total.get(size / 2 - 1) + total.get(size / 2) + 0.0) / 2;
        }
        return j;
    }

}
